#pragma once

#ifndef _SERVER_SYSTEM_SYSTEM_HANDLER_H_
#define _SERVER_SYSTEM_SYSTEM_HANDLER_H_

#include "Core\StanzaHandler.h"

#include "Poco\SharedPtr.h"
#include "Poco\BasicEvent.h"

namespace ExamSystem
{
    using Poco::SharedPtr;
    using Poco::BasicEvent;

    class CPackage;
    class CXMLConnection;

namespace Server
{
    class CServerSystem;
    /**************************************************************************\
     Component:     CSystemHandler
     Description:
        this is a base class for connection handlers
    \**************************************************************************/
    class CSystemHandler : public CStanzaHandler
    {
        friend class CServerSystem;
    public:
        CSystemHandler(CServerSystem *serverSystem, const SharedPtr<CXMLConnection> &connection);
        virtual ~CSystemHandler(void);

    public:
        // start the FSM
        virtual void StartHandler(void) = 0;

        // send a package 
        virtual void SendPackage(CPackage *package);

    protected:
        // stanza handlers
        virtual void startElement(CXMLConnection *connection, const XMLString& uri, const XMLString& localName, const XMLString& qname, const Attributes& attributes);
        virtual void endElement(CXMLConnection *connection, const XMLString& uri, const XMLString& localName, const XMLString& qname);
        virtual void characters(CXMLConnection *connection, const XMLChar ch[], int start, int length);

    protected:
        // register sensitive package types
        virtual void RegisterPackageTypes(void) = 0;
        
        // process received package
        virtual void ProcessPackage(void);

    protected:
        // the server system
        CServerSystem *m_serverSystem;

        // current package
        SharedPtr<CPackage> m_currentPackage;

        // here we directly manage the internal connection
        SharedPtr<CXMLConnection> m_xmlConnection;
    };
}
}


#endif